﻿package com.athena.process
{
	import com.athena.controller.AuctionController;
	import com.athena.controller.LuckyCardController;
	import com.greensock.motionPaths.MotionPath;
	import flash.display.*;
	import flash.utils.*;
	import flash.text.TextFormat;
	import flash.events.*;
	import fl.containers.ScrollPane; 
	import fl.controls.ScrollPolicy; 
	import com.adobe.serialization.json.JSON;
	import flash.net.URLRequest ;
	import flash.net.URLVariables ;
	import flash.net.URLRequestMethod ;
	import flash.net.URLRequestHeader ;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat ;
	import flash.net.sendToURL;
	import flash.net.navigateToURL;
	import gs.*;
	import gs.easing.*;
	/**
	 * ...
	 * @author ...
	 */
	public class LuckyCardFunction 
	{
		public static var TabSelect : int = 1 ;		
		
		public static var scrX35 : ScrollPane = new ScrollPane(); 
		public static var scrollX35 : MovieClip;
		public static var scrX75 : ScrollPane = new ScrollPane(); 
		public static var scrollX75 : MovieClip;	
		
		public static var scrWin : ScrollPane = new ScrollPane(); 
		public static var scrollWin : MovieClip;
		public static var arrX35 : Array = null ;
		public static var arrX75 : Array = null ;
		
		//public static var AGAuction : int = 0 ;
		public static var urlHistoryWin : String = "http://services.athena.vn/ServiceGame/BaimaymanHistoryWin.aspx" ;
		public static var urlLuckyCard : String = "http://services.athena.vn/ServiceGame/Baimayman.aspx" ;		
		public static var feeNum : int = 0 ;
		public static var minuteCount : int = 0 ;
		public static var secondCount : int = 0 ;
		public static var TimerCount:Timer = new Timer(1000);
		public static var TimerWait:Timer = new Timer(1000);
		public static var isLoad : Boolean = false ;
		public static var demLat : int = 0 ;
		public static var AGWin : Number = 0 ;
		
		public static var arrCard : Array = new Array() ;
		public static var arrLat : Array ; 		
		
		public static var objLucky : Object ; 
		public static var isKick : Boolean = false ;
		public static function LuckyCard_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(new Cocaro.Popup_Baimayman) ;
			popup.Choi_btn.addEventListener(MouseEvent.CLICK, Choi_Click) ;
			popup.Gioithieu_btn.addEventListener(MouseEvent.CLICK, Gioithieu_Click) ;
			popup.Top10_btn.addEventListener(MouseEvent.CLICK, Top10_Click) ;
			popup.Close_btn.addEventListener(MouseEvent.CLICK, Close_Click) ;
			popup.Choi_btn.gotoAndStop(2) ;
			popup.Gioithieu_btn.gotoAndStop(1) ;
			popup.Top10_btn.gotoAndStop(1) ;
			popup.Choi_mc.visible = true ;
			popup.Gioithieu_mc.visible = false ;
			popup.Top10_mc.visible = false ;
			popup.Buy_mc.visible = false ;
			popup.Choi_mc.Choingay_btn.addEventListener(MouseEvent.CLICK, Choingay_Click) ;
			popup.Choi_mc.Choitiep_btn.addEventListener(MouseEvent.CLICK, Choitiep_Click) ;			
			popup.Choi_mc.Choingay_btn.visible = false ;
			popup.Choi_mc.Choitiep_btn.visible = false ;
			if (Cocaro.currPlayer.VIP > 1)
				popup.Choi_mc.Fee_txt.text = (Cocaro.currPlayer.VIP -1).toString() ;
			popup.Choi_mc.Second_txt.text = "" ;
			popup.Choi_mc.Minute_txt.text = "" ;
			if (Cocaro.currPlayer.VIP < 2) {
				popup.Choi_mc.Title_txt.text = "CHỨC NĂNG DÀNH RIÊNG CHO CÁC GAME THỦ VIP 2 TRỞ LÊN!";
			}
			isLoad = false ;
			popup.name = "LuckyCard" ;
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;			
			LoadLuckyCard() ;			
		}
		public static function Close_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;
			if (TimerCount.running)
				TimerCount.stop() ;
			if (TimerWait.running)
				TimerWait.stop() ;
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.MarkLQ, 0, false, true) + " LQ";	
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG";	
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatAvatar_MarkLQ) ;
			Cocaro.mainStage_mc.removeChild(popup) ;
		}
		
		public static function Choi_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;
			popup.Choi_btn.gotoAndStop(2) ;
			popup.Gioithieu_btn.gotoAndStop(1) ;
			popup.Top10_btn.gotoAndStop(1) ;
			popup.Choi_mc.visible = true ;			
			popup.Gioithieu_mc.visible = false ;
			popup.Top10_mc.visible = false ;
		}
		public static function Gioithieu_Click(evt : MouseEvent) {			
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;
			popup.Choi_btn.gotoAndStop(1) ;
			popup.Gioithieu_btn.gotoAndStop(2) ;
			popup.Top10_btn.gotoAndStop(1) ;
			popup.Buy_mc.visible = false ;
			popup.Choi_mc.visible = false ;			
			popup.Gioithieu_mc.visible = true ;
			popup.Top10_mc.visible = false ;			
		}
		public static function Top10_Click(evt : MouseEvent) {			
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;
			popup.Choi_btn.gotoAndStop(1) ;
			popup.Gioithieu_btn.gotoAndStop(1) ;
			popup.Top10_btn.gotoAndStop(2) ;
			popup.Buy_mc.visible = false ;
			popup.Choi_mc.visible = false ;			
			popup.Gioithieu_mc.visible = false ;
			popup.Top10_mc.visible = true ;			
			if (!isLoad)
				LoadTop10() ;
		}
		
		public static function LoadLuckyCard() : void {
			var obj : Object = new Object() ;
			obj.evt = "tlucky" ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;			
		}
		public static function StartTimerCount(evt:TimerEvent) : void { //sau 5s duoc bam cap nhat 1 lan
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			secondCount = secondCount - 1 ;
			if (secondCount == -1) {
				secondCount = 59 ;
				minuteCount = minuteCount - 1 ;
				if (minuteCount == -1) {
					minuteCount = 0 ;
					secondCount = 0 ;
				}				
			}
			if (minuteCount < 10)
				popup.Choi_mc.Minute_txt.text = "0" + minuteCount + " :" ; 
			else 
				popup.Choi_mc.Minute_txt.text = minuteCount + " :" ; 
			if (secondCount < 10)
				popup.Choi_mc.Second_txt.text = "0" + secondCount ;
			else 
				popup.Choi_mc.Second_txt.text = secondCount ;
			popup.Choi_mc.Minute_txt.setTextFormat(formatText) ;
			popup.Choi_mc.Second_txt.setTextFormat(formatText) ;
			if (TimerCount.currentCount == TimerCount.repeatCount) {
				TimerCount.stop() ;				
			}
		}
		public static function Buy_Click(evt : MouseEvent) : void {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;				
			//Remove het quan bai
			if (arrLat != null)
				for (var i:int = 0; i < arrLat.length; i++) 
				{				
					var mc : MovieClip = MovieClip(popup.Choi_mc.getChildByName("LuckyCard" + arrLat[i])) ;
					if (mc != null) {
						popup.Choi_mc.removeChild(mc) ;
					}
				}
			//Remove Icon
			var mcIcon : MovieClip = MovieClip(popup.Choi_mc.getChildByName("LuckyIcon"));
			if (mcIcon != null)
				popup.Choi_mc.removeChild(mcIcon) ;
			demLat = 0 ;
			arrLat = new Array() ;
			objLucky = new Object() ;
			var obj : Object = new Object() ;
			obj.evt = "slucky" ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
		}
		//public static function BuyOK(obj : Object) : void {
			//BasicFunction.ShowAlert("Bạn đã mua thành công 1 thẻ may mắn với giá " + (Cocaro.currPlayer.MarkLQ - obj.LQ).toString() + " LQ. Mời bạn chơi tiếp bài may mắn!") ;
			//var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			//if (popup == null) return ;
			//popup.Choi_mc.Choitiep_btn.visible = true ;
		//}
		public static function DisplayData(obj : Object) : void {
			//Cocaro.tf.appendText("==>ABC:" + obj.Cmd + "-" + obj.LQ) ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			var nowTime : Date = new Date(obj.Cmd) ;
			feeNum = obj.P ;
			if (feeNum < 0) feeNum = 0 ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;			
			popup.Choi_mc.Fee_txt.text = feeNum + " lần" ;			
			popup.Choi_mc.Fee_txt.setTextFormat(formatText) ;			
			popup.Choi_mc.Title_txt.text = "HÃY LẬT 5 LÁ BÀI ĐỂ NHẬN NHIỀU GIẢI THƯỞNG HẤP DẪN!";
			popup.Choi_mc.Title_txt.setTextFormat(formatText) ;
			//Cocaro.tf.appendText("=>Timeabc:" + nowTime.getHours() + "-" + nowTime.getMinutes()) ;
			if ((nowTime.getHours() == 15) || (nowTime.getHours() == 21) || (nowTime.getHours() == 11)) {
				if ((nowTime.getMinutes() >= 0) && (nowTime.getMinutes() <= 15)) {
					if (feeNum == 0) {
						popup.Choi_mc.Title_txt.text = "BẠN ĐÃ HẾT LƯỢT CHƠI, HẸN GẶP LẠI BẠN VÀO PHIÊN CHƠI TIẾP THEO!";
						popup.Choi_mc.Title_txt.setTextFormat(formatText) ;
						if (obj.LQ != 0) {
							popup.Buy_mc.visible = true ;
							popup.Buy_mc.Buy_btn.addEventListener(MouseEvent.CLICK, Buy_Click) ;
							popup.Buy_mc.Close_btn.addEventListener(MouseEvent.CLICK, Close_Click) ;
							popup.Buy_mc.Price_txt.text = BasicFunction.numberFormat(obj.LQ,0, false, true) + " LQ" ;
						}
						return ;							
					}
					//Cocaro.tf.appendText("==>Choi") ;
					popup.Choi_mc.Choingay_btn.visible = false ;
					var finishTime : Date = new Date(nowTime.getFullYear(), nowTime.getMonth(), nowTime.getDate(), nowTime.getHours(), 15, 0, 0) ;
					var aTms : Number = Math.floor(finishTime.valueOf() - nowTime.valueOf());
					minuteCount = int(aTms / (       60 * 1000)) % 60  ;
					secondCount = int(aTms / (        1 * 1000)) % 60  ;
					var timeCount : Number = secondCount  + minuteCount * 60;
					//Cocaro.tf.appendText("=>Time:" + timeCount) ;
					if (minuteCount < 10)
						popup.Choi_mc.Minute_txt.text = "0" + minuteCount + " :" ; 
					else 
						popup.Choi_mc.Minute_txt.text = minuteCount + " :" ; 
					popup.Choi_mc.Minute_txt.setTextFormat(formatText) ;
					if (minuteCount < 10)
						popup.Choi_mc.Second_txt.text = "0" + secondCount ;
					else 
						popup.Choi_mc.Second_txt.text = secondCount ;
					popup.Choi_mc.Second_txt.setTextFormat(formatText) ;
					popup.Choi_mc.Choingay_btn.visible = true ;
					if (TimerCount.running)
						TimerCount.stop() ;
					TimerCount = new Timer(1000) ;
					TimerCount.repeatCount = timeCount;
					TimerCount.addEventListener(TimerEvent.TIMER, StartTimerCount);			
					TimerCount.start() ;
				} else {
					popup.Choi_mc.Title_txt.text = "CHƯA ĐẾN GIỜ CHƠI, MỜI BẠN QUAY LẠI VÀO LÚC 11H, 15H HOẶC 21H HÔM NAY!";
					popup.Choi_mc.Title_txt.setTextFormat(formatText) ;
				}
			} else {
				popup.Choi_mc.Title_txt.text = "CHƯA ĐẾN GIỜ CHƠI, MỜI BẠN QUAY LẠI VÀO LÚC 11H, 15H HOẶC 21H HÔM NAY!";
				popup.Choi_mc.Title_txt.setTextFormat(formatText) ;
			}
		}
		
		public static function Choingay_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;			
			demLat = 0 ;
			arrLat = new Array() ;
			objLucky = new Object() ;
			var obj : Object = new Object() ;
			obj.evt = "slucky" ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			popup.Choi_mc.Choingay_btn.visible = false ;
		}
		public static function Choitiep_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;		
			//Remove het quan bai
			if (arrLat != null)
				for (var i:int = 0; i < arrLat.length; i++) 
				{				
					var mc : MovieClip = MovieClip(popup.Choi_mc.getChildByName("LuckyCard" + arrLat[i])) ;
					if (mc != null) {
						popup.Choi_mc.removeChild(mc) ;
					}
				}
			//Remove Icon
			var mcIcon : MovieClip = MovieClip(popup.Choi_mc.getChildByName("LuckyIcon"));
			if (mcIcon != null)
				popup.Choi_mc.removeChild(mcIcon) ;
			demLat = 0 ;
			arrLat = new Array() ;
			objLucky = new Object() ;
			var obj : Object = new Object() ;
			obj.evt = "slucky" ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			popup.Choi_mc.Choitiep_btn.visible = false ;
		}
		public static function Result(objResult : Object) {
			//Cocaro.tf.appendText("==>sLucky:" + objResult.Cmd + "-" + objResult.LQ) ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;
			popup.Buy_mc.visible = false ;
			popup.Choi_mc.Choingay_btn.visible = false ;
			popup.Choi_mc.Choitiep_btn.visible = false ;
			var obj : Object = JSON.decode(objResult.Cmd) ;
			//Cocaro.tf.appendText("=>Arr:" + obj.Arr) ;
			AGWin = obj.agwin ;	
			objLucky = new Object() ;
			objLucky.type = obj.type ;
			objLucky.id = obj.id ;
			Cocaro.currPlayer.MarkLQ = objResult.LQ ;
			//typeLucky = obj.type ;
			arrCard = new Array() ;
			for (var i:int = 0; i < obj.Arr.length; i++) 
			{
				arrCard.push(obj.Arr[i]) ;
			}
			isKick = false ;
			var tChiaBai : Timer = new Timer(30) ;			
			tChiaBai.addEventListener(TimerEvent.TIMER, Start);			
			var dem:Number = 0;
			tChiaBai.repeatCount = 52;
			tChiaBai.start();
			function Start(evt:TimerEvent)
			{
				//Cocaro.tf.appendText("=>" + tChiaBai.currentCount) ;
				var tmpCard : MovieClip = MovieClip(new Cocaro.LuckyCard) ;
				tmpCard.width = 48.8;
				tmpCard.height = 64.75;
				tmpCard.x = 14.85 ;
				tmpCard.y = 90.9 ;
				tmpCard.name = "LuckyCard" + tChiaBai.currentCount ;
				popup.Choi_mc.addChild(tmpCard) ;
				new LuckyCardController(tmpCard, tChiaBai.currentCount) ;
				var doc : int = tChiaBai.currentCount % 13 ;
				if (doc == 0) doc = 13 ;
				var ngang : int = (int)((tChiaBai.currentCount-1) / 13) ;
				moveObj(tmpCard, 14.85 + (doc - 1) * 56.05, 90.9 + (ngang * 70), tChiaBai.currentCount) ;
			}
		}
		
		public static function moveObj(_mc: MovieClip, xNew:Number, yNew:Number, count : int) {				
			TweenMax.to(_mc, 0.5, { x:xNew, y:yNew, ease:Expo.easeOut, onComplete:Latbai() } );
			function Latbai()
			{
				if (count == 52)
					isKick = true ;
			}
		}
		public static function OpenCard(stt : int) {
			if (!isKick) return ;
			var t : Boolean = true ;
			if (arrLat != null) {
				for (var i:int = 0; i < arrLat.length; i++) 
				{
					if (arrLat[i] == stt) {
						t = false ;
						break ;
					}
				}
			}
			if (!t) return ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;
			var mc : MovieClip = MovieClip(popup.Choi_mc.getChildByName("LuckyCard" + stt)) ;
			if (mc == null) return ;
			arrLat.push(stt) ;
			demLat++ ;
			moveObjLat(mc, 228.8 + (demLat - 1) * 70, 365.9) ;			
		}
		public static function moveObjLat(_mc: MovieClip, xNew:Number, yNew:Number) {				
			TweenMax.to(_mc, 0.5, { x:xNew, y:yNew, ease:Expo.easeOut, onComplete:Latbai() } );
			function Latbai()
			{
				_mc.gotoAndStop(arrCard[demLat - 1] + 1) ;
				if (demLat == 5) {
					var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
					if (popup == null) return ;
					//Remove tat ca cac quan bai
					for (var i:int = 0; i < 53; i++) 
					{
						var t : Boolean = true ;
						for (var j:int = 0; j < arrLat.length; j++) 
						{
							if (i+1 == arrLat[j]) {
								t = false ;
								break ;
							}
						}
						if (t) {
							var mc : MovieClip = MovieClip(popup.Choi_mc.getChildByName("LuckyCard" + (i+1).toString())) ;
							if (mc != null) {
								popup.Choi_mc.removeChild(mc) ;
							}							
						}
					}
					TimerWait = new Timer(1000) ;
					TimerWait.repeatCount = 2;
					TimerWait.addEventListener(TimerEvent.TIMER, StartTimerWait);			
					TimerWait.start() ;
				}
			}
		}
		public static function StartTimerWait(evt:TimerEvent) : void { //sau 5s duoc bam cap nhat 1 lan
			if (TimerWait.currentCount == TimerWait.repeatCount) {
				TimerWait.stop() ;	
				var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
				if (popup == null) return ;
				for (var k:int = 0; k < arrLat.length; k++) 
				{
					var mc : MovieClip = MovieClip(popup.Choi_mc.getChildByName("LuckyCard" + arrLat[k])) ;
					moveObjFinish(mc, 205 + k * 70, 250)
				}
				//Cocaro.tf.appendText("=>Type:" + objLucky.type) ;
				//Hien thi ket qua
				var icon : MovieClip ;
				if (objLucky.type == 1)
					icon = MovieClip(new Cocaro.Lucky_Doi) ;
				else if (objLucky.type == 5)
					icon = MovieClip(new Cocaro.Lucky_Thu) ;
				else if (objLucky.type == 20)
					icon = MovieClip(new Cocaro.Lucky_Xam) ;
				else if (objLucky.type == 50)
					icon = MovieClip(new Cocaro.Lucky_Sanh) ;
				else if (objLucky.type == 300)
					icon = MovieClip(new Cocaro.Lucky_Thung) ;
				else if (objLucky.type == 1000)
					icon = MovieClip(new Cocaro.Lucky_Culu) ;
				else if (objLucky.type == 3000)
					icon = MovieClip(new Cocaro.Lucky_Tuquy) ;
				else if (objLucky.type == 10000)
					icon = MovieClip(new Cocaro.Lucky_Thungphasanh) ;
				else icon = MovieClip(new Cocaro.Lucky_Mau) ;
				//if (objLucky.type >= 1) 
					//icon.AG_txt.text = "+ " +  BasicFunction.numberFormat(AGWin, 0, false, true) + " AG" ;
				//Cocaro.tf.appendText("=>ABC:") ;
				icon.x = 235 ;
				icon.y = 330 ;
				icon.name = "LuckyIcon" ;
				popup.Choi_mc.addChild(icon) ;
				var obj : Object = new Object() ;
				obj.evt = "flucky" ;
				obj.id = objLucky.id ;				
				Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
				//Cocaro.tf.appendText("==>SendfLucky:" + JSON.encode(obj)) ;
			}
		}
		public static function moveObjFinish(_mc: MovieClip, xNew:Number, yNew:Number) {				
			TweenMax.to(_mc, 1, { x:xNew, y:yNew, ease:Expo.easeOut, onComplete:Latbai() } );
			function Latbai()
			{				
			}
		}
		public static function LuckyAG(obj : Object) {
			//Cocaro.tf.appendText("=>AG:" + obj.AG + "-" + obj.LQ) ;
			Cocaro.currPlayer.Mark = Cocaro.currPlayer.Mark + obj.AG ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;			
			feeNum = feeNum - 1 ;
			if (feeNum < 0) feeNum = 0 ;
			//Cocaro.tf.appendText("=>feeNum:" + feeNum) ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			popup.Choi_mc.Fee_txt.text = feeNum + " lần" ;			
			popup.Choi_mc.Fee_txt.setTextFormat(formatText) ;			
			if (feeNum > 0)
				popup.Choi_mc.Choitiep_btn.visible = true ;	
			else {
				if (obj.LQ == 0) {
					popup.Choi_mc.Title_txt.text = "BẠN ĐÃ HẾT LƯỢT CHƠI, HẸN GẶP LẠI BẠN VÀO PHIÊN CHƠI TIẾP THEO!";
					popup.Choi_mc.Title_txt.setTextFormat(formatText) ;
				} else {
					popup.Buy_mc.visible = true ;
					popup.Buy_mc.Buy_btn.addEventListener(MouseEvent.CLICK, Buy_Click) ;
					popup.Buy_mc.Close_btn.addEventListener(MouseEvent.CLICK, Close_Click) ;
					popup.Buy_mc.Price_txt.text = BasicFunction.numberFormat(obj.LQ,0, false, true) + " LQ" ;
				}
			}
		}
		/***************************** Ket qua *************************************************/
		public static function Top10Receive(str : String) {
			//Cocaro.tf.appendText("=>Top10Receive:" + str) ;
			var arr : Array = (JSON.decode(str) as Array) ;
			////Cocaro.tf.appendText("=>Length:" + arr.length) ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("LuckyCard")) ;
			if (popup == null) return ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			var dem35 : int = 0 ;
			var dem75 : int = 0 ;
			//Add item moi
			for (var i:int = 0; i < arr.length; i++) 
			{
				var mc : MovieClip = MovieClip(new Cocaro.LuckyCard_Item) ;
				mc.x = 5 ;				
				mc.y = 34 + i * 20 ;
				mc.TT_txt.text = (i + 1).toString() ;
				mc.Name_txt.text = arr[i].N ;
				mc.AG_txt.text = BasicFunction.numberFormat(arr[i].AG, 0, false, true) ;
				mc.Time_txt.text = arr[i].T ;
				mc.name = "LotteryItem" ;
				popup.Top10_mc.addChild(mc) ;
				if (i == 14) break ;
			}
			if (arr.length > 15) {
				for (var i:int = 15; i < arr.length; i++) 
				{
					var mc : MovieClip = MovieClip(new Cocaro.LuckyCard_Item) ;
					mc.x = 389 ;				
					mc.y = 34 + (i-15) * 20 ;
					mc.TT_txt.text = (i - 14).toString() ;
					mc.Name_txt.text = arr[i].N ;
					mc.AG_txt.text = BasicFunction.numberFormat(arr[i].AG, 0, false, true) ;
					mc.Time_txt.text = arr[i].T ;
					mc.name = "LotteryItem" ;
					popup.Top10_mc.addChild(mc) ;
					if (i == 30) break ;
				}
			}
			//Cocaro.tf.appendText("=>Length:" + arr.length) ;
		}
		public static function LoadTop10() {
			//Cocaro.tf.appendText("=>LoadTop10:" + urlHistoryWin) ;
			var request:URLRequest = new URLRequest(urlHistoryWin);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			function onComplete(event:Event):void {
				Top10Receive(event.target.data) ;
			}
		}			
	}
}